package lzzsrj.union.message.redis.producer;

import com.alibaba.fastjson.JSONObject;
import lzzsrj.union.message.common.constant.MessageConstants;
import lzzsrj.union.message.common.producer.Producer;
import lzzsrj.union.message.common.bean.UnionMessage;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Component
public class ProducerRedisMQ implements Producer {

    @Autowired
    private RedissonClient redissonClient;

    public RDelayedQueue getRedissonDelayQueue(String queue) {

//        RDelayedQueue<String> rDelayedQueue = null;

        RBlockingDeque<String> rBlockingDeque = redissonClient.getBlockingDeque(queue);
        if (rBlockingDeque == null) {
            return null;
        }
        return redissonClient.getDelayedQueue(rBlockingDeque);
//        if (rDelayedQueue == null) {
//            return null;
//        }
//        return rDelayedQueue;
//        this.startConsumerDelayQueue();
//        System.out.println(queue + "启动，时间" + LocalDateTime.now());
    }

//    private void startConsumerDelayQueue() {
//
//        Thread thread = new Thread(() -> {
//            while (true) {
//                try {
//
//                    String jsonObject = this.rBlockingDeque.take();
////                    log.info("--> 延迟队列获取数据:{}" + jsonObject);
//
////                    AppointmentInfoDTO appointmentInfoDTO = appointmentService.getAppointmentInfo(Integer.valueOf(jsonObject));
////                    User user = userService.getById(appointmentInfoDTO.getUserId());
/////* 测试用的                    AppointmentInfoDTO appointmentInfoDTO = new AppointmentInfoDTO();
////                    appointmentInfoDTO.setId(90);
////                    appointmentInfoDTO.setChildrenName("zhangsan");
////                    appointmentInfoDTO.setShopName("test");
////                    appointmentInfoDTO.setAppointmentDate("2023-07-27");
////                    appointmentInfoDTO.setAppointmentTime("12:30:00");*/
////
////                    String appointTime = appointmentInfoDTO.getAppointmentDate() + " " + appointmentInfoDTO.getAppointmentStartTime();
////                    Date date = DateUtil.parseDateTime(appointTime);
////                    Long dateToSenond = DateUtil.between(date, new Date(), DateUnit.SECOND);
////
////                    if ((dateToSenond <= 3600 || "1".equals(appointmentSwitch)) &&
////                            (appointmentInfoDTO.getStatus().equals("客服已确认预约") || appointmentInfoDTO.getStatus().equals("客服已修改预约"))) {
////
////                        sendTemplateMsgAppointment(user.getOpenid(), appointmentInfoDTO.getChildrenName(),
////                                appointmentInfoDTO.getShopName(), appointmentInfoDTO.getAppointmentDate(),
////                                appointmentInfoDTO.getAppointmentTime());
////                    }
//
//                } catch (InterruptedException e) {
////                    log.error(e.getMessage());
//                }
//            }
//        });
//        thread.setDaemon(true);
//        thread.start();
//    }


    @Override
    public Boolean send(String topic, String tag, UnionMessage message) {
        return this.send(topic, tag, message, 0L, TimeUnit.SECONDS);
    }

    @Override
    public Boolean send(String topic, String tag, UnionMessage message, Long delay, TimeUnit timeUnit) {
        RDelayedQueue rDelayedQueue = this.getRedissonDelayQueue(MessageConstants.RedisMessage.PREFIX_REDIS_MESSAGE +
                topic + (StringUtils.isEmpty(tag) ? "" : "-" + tag));
        if (rDelayedQueue == null) {
            return Boolean.FALSE;
        }
        rDelayedQueue.offer(JSONObject.toJSONString(message), delay, timeUnit);
        return Boolean.TRUE;
    }
}
